# replicate.house.R
#
# Compare AEH predicted probability of House
# candidate choices to Todorov's actual.
#

cat('\nReplicating House scores ...\n')

dta <- read.csv("house.csv",as.is=TRUE)

# Todorov value is the proportion of respondents who selected
# the winner as more competent.  Recode the variable to the
# proportion of respondents who selected the Democrat as
# more competent.
dem.margin <- dta$dem_pctvote-dta$rep_pctvote
# Replace proportion with 1-p for races that the Republican won
dta$todorov_winner_comp[dem.margin<0] <- 1-dta$todorov_winner_comp[dem.margin<0]

# Plot
our_comp_prop<-pnorm((dta$dem_comp-dta$rep_comp))
x=dta$todorov_winner_comp
y=our_comp_prop

if(toEPS) {postscript("TableAndFiguresOutput/AppendixFigureA.eps",
                        width=6,height=6,horizontal=F)}
par(mar=c(5.1,4.1,2.1,2.1),font.sub=3,las=2)
plot(x,y,
    main="Replication of House Results",
    xlab="Proportion Todorov et al. Respondents Picking Dem More Competent",
    ylab="Predicted Proportion Dem More Competent Given Our Scores",
    pch=19,axes=F,cex=2,
    xlim=c(0,1),ylim=c(0,1))
axis(1);axis(2)
abline(a=0,b=1,lty=2)       # 45 degree line
legend("right",bty='n',legend=paste('r =',round(cor(x,y,
                                'pairwise.complete.obs'),2)) )

if(toEPS){dev.off()}
